package com.da.goal.controller;

import com.da.common.enums.ResultEnum;
import com.da.common.exception.ParameterException;
import com.da.common.utils.DailyAimResult;
import com.da.goal.convert.GoalConverter;
import com.da.goal.entity.DailyGoals;
import com.da.goal.entity.dto.DailyGoalsDTO;

import com.da.goal.service.DailyGoalsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-02-12
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/dailyGoals")
@RequiredArgsConstructor
public class DailyGoalsController {

    private final DailyGoalsService dailyGoalsService;

    @PostMapping("/createdGoal")
    public DailyAimResult<?> createdGoal(@RequestBody DailyGoalsDTO dailyGoalsDTO){
        log.info("Received request to create goal: {}", dailyGoalsDTO);
        Optional.ofNullable(dailyGoalsDTO).orElseThrow(()->new ParameterException(ResultEnum.FAIL.getCode(), "目标不能为空"));
        DailyGoals dailyGoals = GoalConverter.INSTANCE.dtoToEntity(dailyGoalsDTO);
        boolean isCreateGoal = dailyGoalsService.createGoal(dailyGoals);
        if (isCreateGoal){
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(),"创建目标成功！");
        }
        return DailyAimResult.error(ResultEnum.FAIL.getCode(),"创建目标失败！");
    }

    @PostMapping("/updateGoalCompleted")
    public DailyAimResult<?> updateGoalCompleted(@RequestBody DailyGoalsDTO dailyGoalsDTO){
        Optional.ofNullable(dailyGoalsDTO).orElseThrow(()->new ParameterException(ResultEnum.FAIL.getCode(), "目标不能为空"));
        DailyGoals dailyGoals = GoalConverter.INSTANCE.dtoToEntity(dailyGoalsDTO);
        boolean isCreateGoal = dailyGoalsService.updateGoalCompleted(dailyGoals);
        if (isCreateGoal){
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(),"更新目标成功！");
        }
        return DailyAimResult.error(ResultEnum.FAIL.getCode(),"更新目标失败！");
    }






}
